package com.example.smartcity_ikun3.ui.home.adapter

import android.content.Intent
import android.text.Html
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.example.smartcity_ikun3.R
import com.example.smartcity_ikun3.logic.util.Util.guide
import com.example.smartcity_ikun3.logic.util.Util.show
import com.example.smartcity_ikun3.ui.home.activity.NewsShowActivity
import com.example.smartcity_ikun3.ui.home.model.NewsEntity

class ReAdapterNews (
    val data: List<NewsEntity>
) : RecyclerView.Adapter<ReAdapterNews.ViewHolder>(){
    inner class ViewHolder(v: View) : RecyclerView.ViewHolder(v){
        val setTitle: TextView = v.findViewById(R.id.newsTitle)
        val setImageView: ImageView = v.findViewById(R.id.newsImageView)
        val setContent: TextView = v.findViewById(R.id.newsContent)
        val setTime: TextView = v.findViewById(R.id.newsTime)
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        val root = LayoutInflater.from(parent.context).inflate(R.layout.re_home_news, parent, false)
        val view = ViewHolder(root)

        view.itemView.setOnClickListener {
            parent.context.startActivity(Intent(parent.context, NewsShowActivity::class.java).apply {
                putExtra("title", view.setTitle.text.toString())
            })
        }

        return view
    }

    override fun getItemCount(): Int = data.size
    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        val list = data[position]
        holder.setTitle.text = list.newsTitle
        guide(list.newsImageView, holder.setImageView)
        holder.setContent.text = Html.fromHtml(list.newsContent)
        holder.setTime.text = list.newsTime
    }
}